package com.example.rbacmanage.service.app.impl;

import com.example.rbacmanage.entity.app.Activity;
import com.example.rbacmanage.entity.app.AppMenu;
import com.example.rbacmanage.entity.app.MenuCoupon;
import com.example.rbacmanage.entity.system.Menu;
import com.example.rbacmanage.exception.MyException;
import com.example.rbacmanage.mapper.app.ActivityMapper;
import com.example.rbacmanage.mapper.app.AppMenuMapper;
import com.example.rbacmanage.mapper.app.MenuCouponMapper;
import com.example.rbacmanage.mapper.system.MenuMapper;
import com.example.rbacmanage.service.app.ActivityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private MenuCouponMapper menuCouponMapper;
    @Autowired
    private AppMenuMapper appMenuMapper;
    @Override
    public void saveActivity(Activity activity) throws MyException {
        //积分制度和首单折扣只能有一个
        if (activity.getType().equals("0") || activity.getType().equals("1")  || activity.getType().equals("3")){
            Activity act = new Activity();
            act.setType( activity.getType() );
            Integer count = activityMapper.count(act);
            if (count != null && count > 0) {
                throw new MyException("积分制度、首单折扣或者拼团已添加，无须再添加", HttpStatus.INTERNAL_SERVER_ERROR.value());
            }
        }

        activity.setCreateTime(new Date());
        activityMapper.insert(activity);

        if (activity.getMenuIds() != null &&  !activity.getMenuIds().isEmpty()){
            for (Long menuId:activity.getMenuIds()){
                MenuCoupon menuCoupon = new MenuCoupon();
                menuCoupon.setMenuId(menuId);
                menuCoupon.setActivityId(activity.getId());
                menuCoupon.setCreateTime(new Date());
                menuCouponMapper.insert(menuCoupon);
            }
        }
    }

    @Override
    public void save(Activity activity) {
        activity.setCreateTime(new Date());
        activityMapper.insert(activity);
    }

    @Override
    public void remove(Long[] ids) {
        for (Long id : ids) {
            activityMapper.del(id);
        }
    }

    @Override
    public void update(Activity activity) {
        activityMapper.update(activity);
    }

    @Override
    public void updateActivity(Activity activity) throws MyException {
        Activity old = activityMapper.queryOneById(activity.getId());
        if (old != null && !activity.getType().equals(old.getType()) && (activity.getType().equals("0") || activity.getType().equals("1")
                || activity.getType().equals("3"))){
            //积分制度和首单折扣只能有一个
            Activity act = new Activity();
            act.setType( activity.getType() );
            Integer count = activityMapper.count(act);
            if (count != null && count > 0) {
                throw new MyException("积分制度、首单折扣或者拼团已添加，无须再添加", HttpStatus.INTERNAL_SERVER_ERROR.value());
            }
        }
        activityMapper.update(activity);
        if (activity.getMenuIds() != null && !activity.getMenuIds().isEmpty()){
            MenuCoupon menuCoupon = new MenuCoupon();
            menuCoupon.setActivityId(activity.getId());
            List<MenuCoupon> menuCoupons = menuCouponMapper.list(menuCoupon);
            List<Long> alreadyMenuIds = new ArrayList<>();
            for (MenuCoupon menuCoupon1 : menuCoupons) {
                if (activity.getMenuIds().contains(menuCoupon1.getMenuId())) {
                    alreadyMenuIds.add(menuCoupon1.getMenuId());
                    continue;
                }
                activityMapper.del(menuCoupon1.getActivityId());
            }
            for (Long menuId:activity.getMenuIds()){
                if (!alreadyMenuIds.contains(menuId)) {
                    MenuCoupon menuCouponAdd = new MenuCoupon();
                    menuCouponAdd.setMenuId(menuId);
                    menuCouponAdd.setActivityId(activity.getId());
                    menuCouponAdd.setCreateTime(new Date());
                    menuCouponMapper.insert(menuCouponAdd);
                }
            }
        }
    }

    @Override
    public List<Activity> getAll(Activity activity) {
        List<Activity> activities = activityMapper.list(activity);
        for (Activity activity1 : activities) {
            MenuCoupon menuCoupon = new MenuCoupon();
            menuCoupon.setActivityId(activity.getId());
            List<MenuCoupon> menuCoupons = menuCouponMapper.list(menuCoupon);
            List<Long> menuIds = menuCoupons.stream().map(MenuCoupon::getMenuId).distinct().toList();
            if (!menuIds.isEmpty()){
                AppMenu menu = new AppMenu();
                menu.setIds(menuIds);
                List<AppMenu> appMenus = appMenuMapper.list(menu);
                List<String> menuNames = appMenus.stream().map(AppMenu::getMenuName).distinct().toList();
                activity1.setMenuNames(menuNames);
            }
        }
        return activities;
    }

    @Override
    public PageInfo<Activity> getPage(Activity activity) {
        PageHelper.startPage(activity.getPage(),activity.getLimit());
        return new PageInfo<>(getAll(activity));
    }

    @Override
    public Activity getById(Long id) {
        Activity activity = activityMapper.queryOneById(id);
        if (activity != null && activity.getType().equals("2")){
            MenuCoupon menuCoupon = new MenuCoupon();
            menuCoupon.setActivityId(activity.getId());
            List<MenuCoupon> menuCoupons = menuCouponMapper.list(menuCoupon);
            List<Long> menuIds = menuCoupons.stream().map(MenuCoupon::getMenuId).toList();
            activity.setMenuIds(menuIds);
        }
        return activity;
    }

    @Override
    public Integer getCount(Activity activity) {
        return activityMapper.count(activity);
    }
}
